
library(mongolite)
library(plotrix)
library(gridExtra)

Sys.setlocale(locale = "chinese")

get_data <- function(name, usedb = TRUE) {
  if (usedb) {
    m <- mongo(name, "stock", paste("mongodb://", host, sep=""))
    return(m$find())
  } else {
    return(read.csv(paste(base_path, "/", name, ".cvs", sep = "")))
  }
}


parse_today_sz50s <- function(t, sz) {
  x <- data.frame()
  for (i in sz$name) {
    x <- rbind(x, t[which(t$name == i),])
  }
  x <- x[order(-x$changepercent),]
  return(x)
}

parse_companies_from_todaydata <- function(s, e, td, comp) {
  x <- data.frame()
  find_today <- td[td$changepercent > s & td$changepercent < e,]
  for (name in find_today$name) {
    x <- rbind(x, comp[which(comp$name == name),])
  }
  return(x)
}

take_out_companies_high <- function(td, comp) {
  find_today <- td[td$changepercent > 9.9,]
  for (name in find_today$name) {
    find_today$area[find_today$name == name] <- comp[comp$name == name,]$area
    find_today$industry[find_today$name == name] <- comp[comp$name == name,]$industry
  }
  return(find_today)
}

take_out_companies_low <- function(td, comp) {
  find_today <- td[td$changepercent < -9.9,]
  for (name in find_today$name) {
    find_today$area[find_today$name == name] <- comp[comp$name == name,]$area
    find_today$industry[find_today$name == name] <- comp[comp$name == name,]$industry
  }
  return(find_today)
}

draw_hc_area <- function(hc) {
  if (length(hc[,1]) == 0) {
    return(NULL)
  }
  x <- as.data.frame(table(hc$area))
  y <- pie(x$Freq, labels = x$Var1)
  return(y)
}

draw_hc_industry <- function(hc) {
  if (length(hc[,1]) == 0) {
    return(NULL)
  }
  x <- as.data.frame(table(hc$industry))
  y <- pie(x$Freq, labels = x$Var1)
  return(y)
}

radius_1 <- 0.8

draw_all <- function(hc_high, hc_low) {
  par(mfrow=c(2,2), mar=c(.8,.8,.8,.8))
  x <- as.data.frame(table(hc_high$area))
  pie(x$Freq, 
      radius = radius_1,
      col = rainbow(length(x$Freq)),
      labels = paste(x$Var1, x$Freq, sep = "/"), main = "High Area")
  x <- as.data.frame(table(hc_high$industry))
  pie(x$Freq, 
        radius = radius_1,
      col = rainbow(length(x$Freq)),
        labels = paste(x$Var1, x$Freq, sep = "/"), main = "High Industry")
  
  x <- as.data.frame(table(hc_low$area))
  pie(x$Freq, 
      radius = radius_1,
      col = rainbow(length(x$Freq)),
      labels = paste(x$Var1, x$Freq, sep = "/"), main = "Low Area")
  x <- as.data.frame(table(hc_low$industry))
  pie(x$Freq, 
      radius = radius_1,
      col = rainbow(length(x$Freq)),
      labels = paste(x$Var1, x$Freq, sep = "/"), main = "Low Industry")
}

draw_all_hist <- function(hc_high, hc_low) {
  par(mfrow=c(2,2), mar=c(.8,.8,.8,.8))
  x <- as.data.frame(table(hc_high$area))
  a <- ggplot(x, aes(x=Var1, y=Freq), group = factor(1), main = "High Industry") + 
    geom_bar(stat = "identity", width = 0.99, fill=rainbow(length(x$Freq))) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Var1), 
              show.legend = FALSE) +
    labs(title=thedate, x="Level", y="Count")
  
  x <- as.data.frame(table(hc_high$industry))
  b <- ggplot(x, aes(x=Var1, y=Freq), group = factor(1), main = "High Area") + 
    geom_bar(stat = "identity", width = 0.99, fill=rainbow(length(x$Freq))) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Var1), 
              show.legend = FALSE) +
    labs(title=thedate, x="Level", y="Count")
  
  x <- as.data.frame(table(hc_low$area))
  c <- ggplot(x, aes(x=Var1, y=Freq), group = factor(1), main = "Low Area") + 
    geom_bar(stat = "identity", width = 0.99, fill=rainbow(length(x$Freq))) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Var1), 
              show.legend = FALSE) +
    labs(title=thedate, x="Level", y="Count")
  
  x <- as.data.frame(table(hc_low$industry))
  d <- ggplot(x, aes(x=Var1, y=Freq), group = factor(1), main = "Low Area") + 
    geom_bar(stat = "identity", width = 0.99, fill=rainbow(length(x$Freq))) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Var1), 
              show.legend = FALSE) +
    labs(title=thedate, x="Level", y="Count")
  
  grid.arrange(a,b,c,d, ncol = 2, nrow = 2)
}


parse_today_summary <- function(tt) {
  xx <- as.data.frame(table(tt$level))
  xx <- xx[order(as.numeric(xx$Var1)),]
  return(xx)
}

parse_data_summary <- function(t) {
  t$level[t$changepercent >= 9.9] <- "9.11"
  t$level[t$changepercent >= 9 & t$changepercent < 9.9] <- "9.10"
  t$level[t$changepercent >= 8 & t$changepercent < 9] <- "9"
  t$level[t$changepercent >= 7 & t$changepercent < 8] <- "8"
  t$level[t$changepercent >= 6 & t$changepercent < 7] <- "7"
  t$level[t$changepercent >= 5 & t$changepercent < 6] <- "6"
  t$level[t$changepercent >= 4 & t$changepercent < 5] <- "5"
  t$level[t$changepercent >= 3 & t$changepercent < 4] <- "4"
  t$level[t$changepercent >= 2 & t$changepercent < 3] <- "3"
  t$level[t$changepercent >= 1 & t$changepercent < 2] <- "2"
  t$level[t$changepercent > 0 & t$changepercent < 1] <- "1"
  t$level[t$changepercent == 0] <- "0"
  t$level[t$changepercent >= -1 & t$changepercent < 0] <- "-K1"
  t$level[t$changepercent >= -2 & t$changepercent < -1] <- "-J2"
  t$level[t$changepercent >= -3 & t$changepercent < -2] <- "-I3"
  t$level[t$changepercent >= -4 & t$changepercent < -3] <- "-H4"
  t$level[t$changepercent >= -5 & t$changepercent < -4] <- "-G5"
  t$level[t$changepercent >= -6 & t$changepercent < -5] <- "-F6"
  t$level[t$changepercent >= -7 & t$changepercent < -6] <- "-E7"
  t$level[t$changepercent >= -8 & t$changepercent < -7] <- "-D8"
  t$level[t$changepercent >= -9 & t$changepercent < -8] <- "-C9"
  t$level[t$changepercent >= -9.9 & t$changepercent < -9] <- "-B10"
  t$level[t$changepercent <= -9.9] <- "-A11"
  return(t)
}


